package screens;

import java.util.Vector;

import log.Logger;

import test.GradeAnalyzer;

import net.rim.device.api.ui.TransitionContext;
import net.rim.device.api.ui.UiApplication;
import beans.Course;
import beans.Group;
import beans.GroupFile;
import beans.Instance;
import beans.WebCampusFile;

public class Controller{

	// CONTROLLER (this)
	private static Controller controller;

	// WC CHECK PARAMS
	private String username;
	private String password;

	// MESSAGES
	private String loginMessage;
	private String downloadMessage;
	private String commentMessage;

	// DOWNLOAD PARAMS
	private String url;
	private String filename;
	private long size;

	// HASH
	private String hash;

	// PUSH
	private boolean pushStatus;

	private Vector notifications;
	public static int CANT_NOTIFICATIONS = 10;

	private Vector instances;

	// SQL (model)
	//private SQLManager sql;

	//change analyzer

	// grade analyzer
	//private GradeAnalyzer ga;

	// Constructor
	public Controller(){
		this.username="";
		this.password="";
		this.loginMessage="";
		this.downloadMessage="";
		this.commentMessage="";
		this.hash="";
		this.pushStatus = false;
		this.notifications = new Vector();
		this.instances = new Vector();
		//this.sql = new SQLManager();
		//this.ga = new GradeAnalyzer();
	}

	// Singleton
	public static Controller getInstance(){
		if (controller == null){
			controller = new Controller();
		}
		return controller;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public String getLoginMessage() {
		return loginMessage;
	}

	public void setLoginMessage(String loginMessage) {
		this.loginMessage = loginMessage;
	}

	public String getDownloadMessage() {
		return downloadMessage;
	}

	public void setDownloadMessage(String downloadMessage) {
		this.downloadMessage = downloadMessage;
	}

	public String getCommentMessage() {
		return commentMessage;
	}

	public void setCommentMessage(String commentMessage) {
		this.commentMessage = commentMessage;
	}

	// model related methods

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public void saveLogin(String user, String pass){
		this.username=user;
		this.password=pass;
	}

	public Vector getInstancesFromDb(){
		return null;
	}

	public String getHash() {
		return hash;
	}

	public void setHash(String hash) {
		this.hash = hash;
	}

	public void setPushStatus(boolean pushStatus) {
		this.pushStatus = pushStatus;
	}

	public boolean getPushStatus() {
		return pushStatus;
	}

	public void openInstancesScreen(Vector instances) {
		UiApplication.getUiApplication().pushScreen(new InstancesScreen(instances, TransitionContext.TRANSITION_SLIDE));
	}
	public void openCoursesListScreen(Instance i) {
		UiApplication.getUiApplication().pushScreen(new CoursesScreen(i, TransitionContext.TRANSITION_SLIDE));
	}

	public void openGradesListScreen(Course c) {
		UiApplication.getUiApplication().pushScreen(new GradesScreen(c, TransitionContext.TRANSITION_SLIDE));
	}

	public void openConfigScreen() {
		UiApplication.getUiApplication().pushScreen(new ConfigurationScreen());
	}

	public void openNewsScreen(Vector news) {
		UiApplication.getUiApplication().pushScreen(new NewsScreen(news, TransitionContext.TRANSITION_SLIDE));
	}

	public void openDownloadsScreen(Vector folders) {
		UiApplication.getUiApplication().pushScreen(new DownloadsScreen(folders, TransitionContext.TRANSITION_SLIDE));
	}

	public void openFileDownloadScreen(WebCampusFile file) {
		UiApplication.getUiApplication().pushScreen(new FileDownloadScreen(file, TransitionContext.TRANSITION_SLIDE));
	}

	public void openGroupsScreen(Course course) {
		UiApplication.getUiApplication().pushScreen(new GroupScreen(course, TransitionContext.TRANSITION_SLIDE));
	}

	public void openGroupMessagesScreen(Course course) {
		UiApplication.getUiApplication().pushScreen(new GroupMessagesScreen(course, TransitionContext.TRANSITION_SLIDE));
	}

	public void openGroupMessagesScreen(Course course, int transition) {
		UiApplication.getUiApplication().pushScreen(new GroupMessagesScreen(course, transition));
	}

	public void openGroupFilesScreen(Group group) {
		UiApplication.getUiApplication().pushScreen(new GroupFilesScreen(group, TransitionContext.TRANSITION_SLIDE));
	}

	public void openGroupFileDownloadScreen(GroupFile file) {
		UiApplication.getUiApplication().pushScreen(new GroupFileDownloadScreen(file, TransitionContext.TRANSITION_SLIDE));
	}

	public void addNotifications(Vector notifications2) {
		// add all new notifications
		for (int i=0; i<notifications2.size(); i++){
			this.notifications.addElement(notifications2.elementAt(i));
		}
		// remove the oldest ones-
		while(this.notifications.size()>CANT_NOTIFICATIONS){
			this.notifications.removeElementAt(0);
		}
	}

	public Vector getNotifications(){
		return this.notifications;
	}

	public void setInstances(Vector instances) {
		this.instances=instances;
	}

	public Vector getInstances() {
		return instances;
	}

	public void updateInstances(){
		Runnable r = new Runnable() {
			public void run() {
				Logger.log("NOTIFICATION: starting to update instances");
				GradeAnalyzer ga = GradeAnalyzer.getInstance();
				Vector inst = ga.getPeriodosClase(hash);
				if (inst!=null){
					Logger.log("NOTIFICATION: Setting new instances");
					Controller.getInstance().setInstances(inst);
				}
			}
		};
		r.run();
	}

	// costo: O(h2)
	public Course getCourseFromId(String idAula){
		for (int i=0; i<this.instances.size(); i++){
			Instance ins = (Instance) this.instances.elementAt(i);
			for (int c=0; c<ins.getCourses().size(); c++){
				Course cou = (Course) ins.getCourses().elementAt(c);
				if (cou.getIDAula().equals(idAula)){
					return cou;
				}
			}
		}
		// not found
		return null;
	}


}
